extends NinePatchRect


onready var box = $ScrollContainer/HBoxContainer
onready var container = $ScrollContainer

var ori = "" #主職業

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

func init(ori):
	self.ori = ori
	$NinePatchRect/Label.text = ori
	sys.addTip($NinePatchRect,"职业会影响可以学到的技能")
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
func addCha(item):	
	var bt = preload("res://tscn/chara/charaItem.tscn").instance()
	box.add_child(bt)
	bt.init(item)
	bt.get_node("popLv").hide()
	bt.get_node("name").hide()
	_layout()
	if item.lock == 1 or not sys.main.ui.has_node("bookDlg"): return
	var lbt = preload("res://tscn/book/lock.tscn").instance()
	bt.add_child(lbt)
	lbt.init(item)
	lbt.position = Vector2(bt.rect_size.x * 0.5,bt.rect_size.y - 16)
	
	
func sortCha():
	var chas = box.get_children()
	chas.sort_custom(self,'sortChildren')
	for c in chas:
		box.move_child(c,chas.find(c))
	
	
func sortChildren(a,b):
	var p1 = a.cha.getOrientation()
	var p2 = b.cha.getOrientation()
	#沒有副職業的在前
	if not p1 :
		return true 
	if not p2 :
		return false 
	return p1 > p2
		
	
func _layout():
	var btns = box.get_children()
	var num = 0 
	for b in btns:
		if b.is_visible():
			num = num + 1 
	var lenNum = ceil(num * 1.0 / box.get_columns())
	var baseLen = 40
	set_custom_minimum_size(Vector2(550,baseLen + 100 * (lenNum)))

func _process(delta):
	pass	
	
